package com.example.demo.web;

import com.example.demo.untils.CheckoutUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Controller
public class DemoController {

   /* @RequestMapping
    public String hello(@RequestBody Params params){

        return "Hello world!\n["+params+"]";
    }*/

    @ResponseBody
    @GetMapping("/auth")
    public void auth(@RequestParam("code") String code, @RequestParam("state") String state){
        log.info("auth开始了。。。。");
        log.info("code={}",code);
        log.info("state={}",state);
    }

    @ResponseBody
    @GetMapping("/check")
    public String check(HttpServletRequest request ){
        // 微信加密签名
        String signature = request.getParameter("signature");
        // 时间戳
        String timestamp = request.getParameter("timestamp");
        // 随机数
        String nonce = request.getParameter("nonce");
        // 随机字符串
        String echostr = request.getParameter("echostr");
        // 通过检验signature对请求进行校验，若校验成功则原样返回echostr，表示接入成功，否则接入失败
        log.info("echostr={}",echostr);

        boolean bool = CheckoutUtil.checkSignature(signature, timestamp, nonce);

        log.info("checkSignature={}",bool);

        /*if (signature != null && bool) {
            return echostr;
        }*/

        return echostr;
    }

    @RequestMapping
    public String index(){

        return "index";
    }

}
